package com.bit.springaopdemo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Slf4j
@Aspect
//@Component
public class AspectDemo {
    //公共的切点表达式
    @Pointcut("execution (* com.bit.springaopdemo.controller.*.*(..))")
    private void path(){}

    @Before("path()")
    public void doBefore(){
        log.info("执行doBefore方法");
    }
    @After("path()")
    public void doAfter(){
        log.info("执行doAfter方法");
    }
    @AfterReturning("execution (* com.bit.springaopdemo.controller.*.*(..))")
    public void doAfterReturning(){
        log.info("执行doAfterReturning方法");
    }
    @AfterThrowing("execution (* com.bit.springaopdemo.controller.*.*(..))")
    public void doAfterThrowing(){
        log.info("执行doAfterThrowing方法");
    }

    @Around("execution (* com.bit.springaopdemo.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint){
        log.info("Around方法开始执行");
        Object proceed = null;
        try{
           proceed = joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally{
            log.info("Around方法执行结束");
        }
//        Object proceed = joinPoint.proceed();
//        log.info("Around方法执行结束");
        return proceed;
    }
}
